package piis;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.swing.JFrame;

import org.jfree.ui.RefineryUtilities;

import Algorithms.LinearRegression;
import Algorithms.LinearRegressionCrossValidation;
import Algorithms.LinearRegressionWithRegularization;

public class test22 {
        
        
        
        static String path = "src/test2";
        static int scalFactor = 10;
        
        public static void main(String[] args) {
                
                File dir = new File(path);
                String[] files = dir.list();
                
                if (files == null)
                          System.out.println("Empty directory");
                        else { 
                          for (int x=0;x<files.length;x++)
                            if(files[x].endsWith(".in") && files[x].startsWith("linreg_gd_ts_1_5"))
                                prepareAndInvoke(files[x]);
                        }
                
                
                
        }
        
        public static void prepareAndInvoke(String name){
                
                try {
                        Scanner sc = new Scanner(new File(path + "/" + name));
                        
                        int p = sc.nextInt();   
                        double min = Double.valueOf(sc.next());
                        double max = Double.valueOf(sc.next());
                        ArrayList<Double> x = new ArrayList<Double>();
                        ArrayList<Double> y = new ArrayList<Double>();
                        while(sc.hasNext()){
                                x.add(Double.valueOf(sc.next()));
                                y.add(Double.valueOf(sc.next()));
                        }
                        
                        linealRegression(p, x.toArray(new Double[x.size()]), y.toArray(new Double[y.size()]), name,  min, max);
                        sc.close();
                        
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                }
        }
        
        public static void linealRegression(int p, Double[] x, Double[] y, String name, double min, double max){
                
               
                
                double[][] xm = new double[p][x.length];
                double[] finaly = new double[y.length];
                double[][] finalyy = new double[1][y.length];

                for(int i=1; i <= p ; i++){
                        for(int j=0;j<x.length; j++){
                                xm[i-1][j] = Math.pow(x[j]/scalFactor, i);
                        }
                }
                
                for(int i = 0; i<y.length;i++){
                	finaly[i]=y[i]/scalFactor;
                	finalyy[0][i]=y[i];
                }

        
//                LinearRegressionWithRegularization linearR = new LinearRegressionWithRegularization(xm,finaly,p+1);
//	              LinearRegression linearR = new LinearRegression(xm,finaly,p+1);


//                System.out.println(xm[0].length % 2 == 0);
//	              linearR.gradientDescent();
                  LinearRegressionCrossValidation linearR = new LinearRegressionCrossValidation(xm, finaly, p+1);
                  linearR = (LinearRegressionCrossValidation) linearR.getOptimun();
        
	       double[] errors = linearR.getErrors();
	
	//       double[] params = linearR.getParams();
	//              
	//       System.out.println(params);
	  
	
	       String newname = name.replace(".in", "");
	       
	      Map<Double,Double> map = new HashMap<>();
	       for(double j = min; j <= max; j+=0.1){
	        map.put(Double.valueOf(j), linearR.func(j/scalFactor)*scalFactor);
	       }
	       
	       double[] finalx = new double[x.length];  
	       for(int i = 0; i<y.length;i++){
	        finalx[i]=x[i];
	       }
	       
	       double[][] finalxx = new double[1][x.length];
	       finalxx[0] = finalx;
	
	      
	       
	       // Create a plot.
	       Plot demo = new Plot("Assignment 3", newname + ": Grade = " + p ,map,finalyy,finalxx);
	       demo.pack();
	       RefineryUtilities.centerFrameOnScreen(demo);
	       demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	       demo.setVisible(true);
	      
	       
	       
	       
	        try{
	                FileWriter errorsF = new FileWriter(path + "/" + newname + "_train.out");
	                             
	                for(int i = 0; i<errors.length; i++){
	                        errorsF.write(errors[i] + "\r\n");
	                }
	                
	                errorsF.close();
	                
	        }catch(Exception e){
	                e.printStackTrace();
	        }
	     
	        try{
	                FileWriter paramsF = new FileWriter(path + "/" + newname + "_result.out");
	                             
	                for(double j = min; j <= max ; j+=0.1){
	                        paramsF.write(j + " " + linearR.func(j/scalFactor)*scalFactor + "\r\n");
	                }
	                
	              paramsF.close();
	                
	        }catch(Exception e){
	                e.printStackTrace();
	        }
                
        }

}